package cetcbigdata.da.subject_integration_service.guangdong;

import cetcbigdata.da.base.Base;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class GuangDongLegal extends Base {
    private static String baseRegion = "广东省";

    private static String baseModule = "法人服务";

    @Override
    public JSONObject getDataDemo() {
        try {
            return processResult("xxx", "xxx", "xxx", "xxx", new JSONObject(), "ss", "ss");
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {
        try {
            for(JSONObject r : getRegion()){
                for(JSONObject m : getModule()){
                    this.parseListPage(recordSender, fieldsList, r, m);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private JSONObject parseListPage(RecordSender recordSender, List<Object> fieldsList, JSONObject regionItem, JSONObject moduleItem){
        //获取各区编码和类型编码
        String code =  moduleItem.getString("code");
        String regCode = regionItem.getString("code");

        //获取各个区名和类型名称
        String region = baseRegion + "-" + regionItem.getString("name");
        String module = baseModule + "-" + moduleItem.getString("name");
        try{
            // 初始化总页码数
            int pages = 0;
            // 当前页
            int currentPage = 1;
            // 如果当前也不等于总页码数，则一直循环 ，实现翻页
            while (true){
                String listUrl = "https://www.gdzwfw.gov.cn/portal/item/special/themeitem/list?PAGE_NUM=" +
                        currentPage + "&PAGE_SIZE=10&TASKTAG=2&AREA_CODE=" + regCode + "&TOPIC_TYPE=" + code + "&ISLOCALLEVEL=0&IS_ONLINE=";
                JSONObject headers = new JSONObject();
                headers.put("Referer", "https://apps.gdzwfw.gov.cn/");
                headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0");
                JSONObject parmas = new JSONObject();
                parmas.put("PAGE_NUM", currentPage);
                parmas.put("PAGE_SIZE", 10);
                parmas.put("TASKTAG", 2);
                parmas.put("AREA_CODE", regCode);
                parmas.put("TOPIC_TYPE", code);
                parmas.put("ISLOCALLEVEL", 0);
                parmas.put("IS_ONLINE", "");
                String listResponse =  getReq(listUrl,headers,parmas);
                JSONObject responseObj = JSONObject.parseObject(listResponse);
                if(responseObj.getString("statusCode").equals("200")){
                    JSONArray listTemp = responseObj.getJSONObject("data").getJSONArray("data");
                    JSONObject listPageObj = new JSONObject();
                    for(Object o : listTemp){
                        JSONObject item = (JSONObject) o;
                        String taskCode = item.getString("taskCode");
                        if(listPageObj.containsKey(taskCode)){
                            listPageObj.getJSONArray(taskCode).add(item);
                        }else {
                            listPageObj.put(taskCode, new JSONArray());
                            listPageObj.getJSONArray(taskCode).add(item);
                        }
                    }

                    // 每页每项遍历
                    for(String key : listPageObj.keySet() ){
                        JSONObject contentParent =new JSONObject();
                        String title = null;
                        List<JSONObject> contentList = new ArrayList<>();
                        String detailUrl = null;
                        String contentResponse = null;
                        for(Object o : listPageObj.getJSONArray(key)){
                            JSONObject item = (JSONObject) o;
                            detailUrl = String.format("https://www.gdzwfw.gov.cn/portal/api/v2/impl-subject/get-detail?service_code=%s",
                                    item.getString("taskCode"));
                            title = item.getString("taskName");

                            // 请求详情页
                            JSONObject contentChild =new JSONObject();
                            contentResponse = getReq(detailUrl, headers, parmas);
                            JSONObject baseInfo = Gutil.getBaseInfo(contentResponse);
                            JSONObject scopesInfo = Gutil.getScopesInfo(contentResponse);
                            JSONObject handlingProcedures = Gutil.getHandlingProcedures(contentResponse);
                            List<JSONObject> materialAll = Gutil.getMaterialAll(contentResponse);
                            JSONObject otherInfo = Gutil.getOtherInfo(contentResponse);
                            JSONArray chargeInfo =  Gutil.getChargeInfo(contentResponse);
                            contentChild.put("基本信息", baseInfo);
                            contentChild.put("受理标准", scopesInfo);
                            contentChild.put("办理流程", handlingProcedures);
                            contentChild.put("申请材料", materialAll);
                            contentChild.put("其他信息", otherInfo);
                            contentChild.put("收费信息", chargeInfo);
                            contentList.add(contentChild);
                        }
                        contentParent.put("名称", title);
                        contentParent.put("选项", contentList);
                        return processResult(listUrl, listResponse,detailUrl,contentResponse, contentParent,
                                region,
                                module
                        );

                    }
                }else {
                    return new JSONObject();
                }
                // 更新当前页
                currentPage = responseObj.getJSONObject("data").getInteger("CURRENTPAGEINDEX") + 1;
                // 更新总页数
                int total = responseObj.getJSONObject("data").getInteger("TOTALNUM");
                int size = responseObj.getJSONObject("data").getInteger("PAGESIZE");
                int prePages = total/size;
                pages = total%size == 0 ? prePages : prePages + 1;
                if (currentPage > pages){
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JSONObject();
    }



    //获取市名和各市编码
    private static JSONArray getCityList(){
        String city = " [\n" +
                "{\"name\":\"广州市\",\"region\":\"440100\"},    \n" +
                "{\"name\":\"深圳市\",\"region\":\"440300\"}, \n" +
                "{\"name\":\"珠海市\",\"region\":\"440400\"},\n" +
                "{\"name\":\"汕头市\",\"region\":\"440500\"},\n" +
                "{\"name\":\"佛山市\",\"region\":\"440600\"},\n" +
                "{\"name\":\"韶关市\",\"region\":\"440200\"},\n" +
                "{\"name\":\"河源市\",\"region\":\"441600\"},\n" +
                "{\"name\":\"梅州市\",\"region\":\"441400\"},\n" +
                "{\"name\":\"惠州市\",\"region\":\"441300\"},\n" +
                "{\"name\":\"汕尾市\",\"region\":\"441500\"},\n" +
                "{\"name\":\"东莞市\",\"region\":\"441900\"},\n" +
                "{\"name\":\"中山市\",\"region\":\"442000\"},\n" +
                "{\"name\":\"江门市\",\"region\":\"440700\"},\n" +
                "{\"name\":\"阳江市\",\"region\":\"441700\"},\n" +
                "{\"name\":\"湛江市\",\"region\":\"440800\"},\n" +
                "{\"name\":\"茂名市\",\"region\":\"440900\"},\n" +
                "{\"name\":\"肇庆市\",\"region\":\"441200\"},\n" +
                "{\"name\":\"揭阳市\",\"region\":\"445200\"},\n" +
                "{\"name\":\"清远市\",\"region\":\"441800\"},\n" +
                "{\"name\":\"潮州市\",\"region\":\"445100\"},\n" +
                "{\"name\":\"云浮市\",\"region\":\"445300\"},\n" +
//                "{\"name\":\"横琴粤澳深度合作区\",\"region\":\"440405\"}\n" +
                " ]";
        return JSONArray.parseArray(city);
    }

    private static List<JSONObject> getRegion() {
        //获取各市编码
        JSONArray cityList = getCityList();
        List<JSONObject> out = new ArrayList<>();
        for (Object o : cityList){
            JSONObject oo = (JSONObject) o;
            String code = oo.getString("region");

            String url = "https://www.gdzwfw.gov.cn/portal/custom-config/gdbsNav/getDetail";
            JSONObject header = new JSONObject();
            header.put("Referer", "https://apps.gdzwfw.gov.cn/");
            header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0");
            JSONObject params = new JSONObject();
            params.put("regCode", code);

            //获取各个市的所有区的源数据
            String response = postFormReq(url, header, params);
            JSONObject responseObj = JSONObject.parseObject(response);
            if (responseObj.getString("statusCode").equals("200")){
                JSONArray dataList = responseObj.getJSONObject("data").getJSONObject("data").getJSONArray("country");

                //判断各个市是否包含区
                if (dataList.size() != 0){
                    for (Object d : dataList){
                        JSONObject dd = (JSONObject) d;
                        JSONObject outItem = new JSONObject();
                        String name = oo.getString("name") + "-" + dd.getString("ORGSNAME");
                        outItem.put("name", name);
                        outItem.put("code", dd.getString("ORGAREACODE"));
                        out.add(outItem);
                    }
                }else {
                    JSONObject outItem = new JSONObject();
                    String name = oo.getString("name");
                    outItem.put("name", name);
                    outItem.put("code", oo.getString("region"));
                    out.add(outItem);
                }
            }
        }
        return out;
    }

    private static List<JSONObject> getModule(){
        //获取类型名称和编码
        String data = "{\"THEMES\":[{\"CODE\":\"002001001\",\"NAME\":\"准营准办\",\"ORDERNUM\":2},{\"CODE\":\"002001002\",\"NAME\":\"设立变更\",\"ORDERNUM\":3},{\"CODE\":\"002001004\",\"NAME\":\"资质认证\",\"ORDERNUM\":5},{\"CODE\":\"002001005\",\"NAME\":\"对外交流\",\"ORDERNUM\":6},{\"CODE\":\"002001006\",\"NAME\":\"司法公证\",\"ORDERNUM\":7},{\"CODE\":\"002001007\",\"NAME\":\"金融投资\",\"ORDERNUM\":8},{\"CODE\":\"002001008\",\"NAME\":\"知识产权\",\"ORDERNUM\":9},{\"CODE\":\"002001011\",\"NAME\":\"质量检查\",\"ORDERNUM\":10},{\"CODE\":\"002001012\",\"NAME\":\"安全防护\",\"ORDERNUM\":11},{\"CODE\":\"002001013\",\"NAME\":\"环保绿化\",\"ORDERNUM\":12},{\"CODE\":\"002001014\",\"NAME\":\"建设管理\",\"ORDERNUM\":13},{\"CODE\":\"002001015\",\"NAME\":\"土地房产\",\"ORDERNUM\":14},{\"CODE\":\"002001016\",\"NAME\":\"人力资源\",\"ORDERNUM\":15},{\"CODE\":\"002001017\",\"NAME\":\"税收财务\",\"ORDERNUM\":16},{\"CODE\":\"002001018\",\"NAME\":\"社会保障\",\"ORDERNUM\":17},{\"CODE\":\"002001019\",\"NAME\":\"新闻广电\",\"ORDERNUM\":18},{\"CODE\":\"002001020\",\"NAME\":\"破产注销\",\"ORDERNUM\":19},{\"CODE\":\"002001021\",\"NAME\":\"其他\",\"ORDERNUM\":20},{\"CODE\":\"002001022\",\"NAME\":\"交通运输\",\"ORDERNUM\":21},{\"CODE\":\"002001023\",\"NAME\":\"立项审批\",\"ORDERNUM\":22},{\"CODE\":\"002001024\",\"NAME\":\"农林牧渔\",\"ORDERNUM\":23},{\"CODE\":\"002001025\",\"NAME\":\"水利水务\",\"ORDERNUM\":24},{\"CODE\":\"002001026\",\"NAME\":\"文物保护\",\"ORDERNUM\":25},{\"CODE\":\"002001027\",\"NAME\":\"医疗卫生\",\"ORDERNUM\":26},{\"CODE\":\"002001028\",\"NAME\":\"抵押质押\",\"ORDERNUM\":27},{\"CODE\":\"002001029\",\"NAME\":\"商务贸易\",\"ORDERNUM\":28},{\"CODE\":\"002001030\",\"NAME\":\"招标拍卖\",\"ORDERNUM\":29},{\"CODE\":\"002001031\",\"NAME\":\"海关口岸\",\"ORDERNUM\":30},{\"CODE\":\"002001032\",\"NAME\":\"涉外服务\",\"ORDERNUM\":31},{\"CODE\":\"002001033\",\"NAME\":\"应对气候变化\",\"ORDERNUM\":32},{\"CODE\":\"002001034\",\"NAME\":\"科技创新\",\"ORDERNUM\":33},{\"CODE\":\"002001035\",\"NAME\":\"文体教育\",\"ORDERNUM\":34},{\"CODE\":\"002001036\",\"NAME\":\"民族宗教\",\"ORDERNUM\":35},{\"CODE\":\"002001037\",\"NAME\":\"检验检疫\",\"ORDERNUM\":36},{\"CODE\":\"002001038\",\"NAME\":\"安全生产\",\"ORDERNUM\":37},{\"CODE\":\"002001039\",\"NAME\":\"公安消防\",\"ORDERNUM\":38},{\"CODE\":\"002001040\",\"NAME\":\"公用事业\",\"ORDERNUM\":39},{\"CODE\":\"002001041\",\"NAME\":\"法人注销\",\"ORDERNUM\":40},{\"CODE\":\"002001042\",\"NAME\":\"城市综合执法\",\"ORDERNUM\":41},{\"CODE\":\"002001043\",\"NAME\":\"食品药品\",\"ORDERNUM\":42},{\"CODE\":\"002001044\",\"NAME\":\"投资审批\",\"ORDERNUM\":43},{\"CODE\":\"002001045\",\"NAME\":\"国土和规划建设\",\"ORDERNUM\":44},{\"CODE\":\"002001046\",\"NAME\":\"质量技术\",\"ORDERNUM\":45},{\"CODE\":\"002001047\",\"NAME\":\"档案文物\",\"ORDERNUM\":46},{\"CODE\":\"002001048\",\"NAME\":\"融资信贷\",\"ORDERNUM\":47},{\"CODE\":\"002001049\",\"NAME\":\"水务气象\",\"ORDERNUM\":48},{\"CODE\":\"002001050\",\"NAME\":\"地方特色分类\",\"ORDERNUM\":49},{\"CODE\":\"002001051\",\"NAME\":\"财务税务\",\"ORDERNUM\":50},{\"CODE\":\"002001052\",\"NAME\":\"劳动保障\",\"ORDERNUM\":51},{\"CODE\":\"002001053\",\"NAME\":\"年审年检\",\"ORDERNUM\":52}]}";
        JSONArray validData = JSONObject.parseObject(data).getJSONArray("THEMES");
        List<JSONObject> out = new ArrayList<>();
        for(Object o : validData){
            JSONObject oItem = (JSONObject) o;
            JSONObject outItem = new JSONObject();
            outItem.put("code", oItem.getString("CODE"));
            outItem.put("name", oItem.getString("NAME"));
            out.add(outItem);
        }
        return out;
    }

    public static void main(String[] args) {
        GuangDongLegal spider = new GuangDongLegal();
        spider.getData(null, new ArrayList<>());
    }

}
